package com.project.petLossSystem.controller.admin;

import com.project.petLossSystem.common.ServiceResultEnum;
import com.project.petLossSystem.po.AddressCategory;
import com.project.petLossSystem.po.PetCategory;
import com.project.petLossSystem.service.AddressCategoryService;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.Result;
import com.project.petLossSystem.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AddressCategoryController {

    @Autowired
    private AddressCategoryService addressCategoryService;

    @GetMapping("/addressCategories")
    public String addressCategory() {
        return "/pc/admin/address_categories";
    }

    @GetMapping("/addressCategories/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("page")) || ObjectUtils.isEmpty(params.get("rows")) || ObjectUtils.isEmpty(params.get("parentId"))) {
            return ResultGenerator.failureResult("参数异常！");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        // 因为是表格用的数据，所以不写失败
        return ResultGenerator.successResult(addressCategoryService.getAddressPage(pageQueryUtil));
    }

    @PostMapping("/addressCategories/save")
    @ResponseBody
    public Result save(@RequestBody AddressCategory addressCategories) {
        // 判断必须参数
        if (!StringUtils.hasText(addressCategories.getAddressName()) ||
                ObjectUtils.isEmpty(addressCategories.getParentId()) ||
                ObjectUtils.isEmpty(addressCategories.getAddressLevel())) {
            return ResultGenerator.failureResult("参数异常！");
        }
        // 处理地址名字符串 多个地址的字符串（空格隔开）
        String[] addressNames = addressCategories.getAddressName().split(" ");
        // 结果判断
        String resultMsg = addressCategoryService.insertAddresses(addressCategories.getParentId(), addressCategories.getAddressLevel(), addressNames);
        if(resultMsg.equals(ServiceResultEnum.SUCCESS.getResult())){
            return ResultGenerator.successResult();
        }else return ResultGenerator.failureResult(resultMsg);
    }

    @PutMapping("/addressCategories/update")
    @ResponseBody
    public Result update(@RequestBody AddressCategory addressCategory) {
        // 判断必须参数，不必要分类等级，因为前端修改无法修改等级
        if (ObjectUtils.isEmpty(addressCategory.getAddressId()) ||
                !StringUtils.hasText(addressCategory.getAddressName()) ||
                ObjectUtils.isEmpty(addressCategory.getParentId())) {
            return ResultGenerator.failureResult("参数异常！");
        }
        String resultMsg = addressCategoryService.updateAddress(addressCategory);
        if (resultMsg.equals(ServiceResultEnum.SUCCESS.getResult())) {
            return ResultGenerator.successResult();
        } else return ResultGenerator.failureResult(resultMsg);
    }

    // 删除需要删除旗下所有子地址
    @PutMapping("/addressCategories/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] addressId) {
        // 判断参数，如果是null是映射不上的，只需判断长度
        if (addressId.length < 1) {
            return ResultGenerator.failureResult("参数异常！");
        }
        AddressCategory firstAddress = addressCategoryService.getAddressById(addressId[0]);
        // 这里记得得new，因为数组直接转的list是数组的内部类，不是util下的
        List<Integer> idList = new ArrayList<>(Arrays.asList(addressId)); // 删除的全存
        // 根据级别插入子地址
        if (firstAddress.getAddressLevel() == 1) {
            // 获取二级分类
            List<Integer> secondIdList = addressCategoryService.getIdListByParentIds(idList);
            if(!CollectionUtils.isEmpty(secondIdList)){
                idList.addAll(secondIdList);
                // 获取三级分类
                List<Integer> thirdIdList = addressCategoryService.getIdListByParentIds(secondIdList);
                idList.addAll(thirdIdList);
            }
        } else if (firstAddress.getAddressLevel() == 2) {
            // 获取三级分类
            List<Integer> secondIdList = addressCategoryService.getIdListByParentIds(idList);
            idList.addAll(secondIdList);
        }
        // 更新deleted
        if (addressCategoryService.updateDeleted(idList)) {
            return ResultGenerator.successResult();
        } else return ResultGenerator.failureResult("删除失败！");
    }
}
